import { ref } from 'vue'
import { defineStore } from 'pinia'
import { useMenuStore } from './menu'

export const useUserStore = defineStore(
  'user',
  () => {
    const token = ref('')
    const userInfor = ref({})
    const loginUserType = ref('') //YWYH/WHYH
    const setToken = (TOKEN) => {
      token.value = TOKEN
    }
    const setUserInfor = (loginUser) => {
      for (const key in loginUser) {
        userInfor.value[key] = loginUser[key]
      }
      userInfor.value.dwdm = loginUser.currentUnitCode
      userInfor.value.dwmc = loginUser.currentUnitMc
      userInfor.value.dwlx = loginUser.currentUnitCode.slice(7, 8)
    }
    const clearUserInfor = () => {
      token.value = ''
      loginUserType.value = ''
      userInfor.value = {}
      const menuStore = useMenuStore()
      menuStore.iframeSrc = ''
      menuStore.setLeftMenu([])
      menuStore.setTopMenu([])
    }
    return {
      userInfor,
      token,
      loginUserType,
      setToken,
      setUserInfor,
      clearUserInfor
    }
  },
  {
    persist: true
  }
)
